package com.dcy.znoa.controller.sys;

import com.alibaba.fastjson.JSON;
import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.sys.model.operRole.AddOperRoleModel;
import com.dcy.znoa.controller.sys.model.operRole.SelectOperRoleModel;
import com.dcy.znoa.domain.sys.OperRole;
import com.dcy.znoa.service.sys.OperRoleService;
import com.mysql.fabric.xmlrpc.base.Data;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

/**
 * @author chenjie
 */
@Controller
@RequestMapping("operRole")
public class OperRoleController extends BaseController {
    @Autowired
    OperRoleService operRoleService;

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/selectList")
    public JsonForList selectList(SelectOperRoleModel model) throws BusinessException, InvocationTargetException, IllegalAccessException {
        //查询该oper所属角色明细：非roleId
        return operRoleService.selectList(model);    
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/insert")
    //可批量插入：插入单个员工的角色
    public JsonCommon insert(@Valid AddOperRoleModel model,@RequestParam String roleIdsJsonStr) throws BusinessException, InvocationTargetException, IllegalAccessException {
        
    	List<Long> idsList = JSON.parseArray(roleIdsJsonStr,Long.class);
    	if (!idsList.isEmpty()) {
    		for (Long roleId : idsList) {
    			OperRole operRole = new OperRole();
                BeanUtils.copyProperties(operRole, model);
                operRole.setCreateOperId(getCurrentUser().getOperId());
                operRole.setRoleId(roleId);
                operRoleService.insert(operRole);
			}    		          
		}
        
        return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/insertByRoleId")
    //可批量插入：插入角色所属员工
    public JsonCommon insertByRoleId(@RequestParam Long roleId,@RequestParam String userIdsJsonStr) throws BusinessException, InvocationTargetException, IllegalAccessException {
        
    	List<Long> idsList = JSON.parseArray(userIdsJsonStr,Long.class);
    	if (!idsList.isEmpty()) {
    		Date createTime = new Date();
    		for (Long userId : idsList) {
    			OperRole operRole = new OperRole();
                operRole.setCreateOperId(getCurrentUser().getOperId());
                operRole.setRoleId(roleId);
                operRole.setOperId(userId);
                operRole.setCreateTime(createTime);
                operRole.setValidFlag(1);
                operRoleService.insert(operRole);
			}    		          
		}
        
        return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
    }
    
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/delete")
    //可批量物理删除
    public JsonCommon delete(@RequestParam Long operId ,@RequestParam String roleIdsJsonStr) throws BusinessException, InvocationTargetException, IllegalAccessException {           	
    	List<Long> idsList = JSON.parseArray(roleIdsJsonStr,Long.class);
    	if (!idsList.isEmpty()) {
    		 return operRoleService.delete(operId ,idsList);	          
		}else {
			return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
		}
       
    }

    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/deleteByRoleId")
    //可批量物理删除:通过角色批量删除所属员工
    public JsonCommon deleteByRoleId(@RequestParam Long roleId,@RequestParam String userIdsJsonStr) throws BusinessException, InvocationTargetException, IllegalAccessException {           	
    	List<Long> idsList = JSON.parseArray(userIdsJsonStr,Long.class);
    	if (!idsList.isEmpty()) {
    		 return operRoleService.deleteByRoleId(roleId ,idsList);	          
		}else {
			return new JsonCommon(JsonCommon.SUCCESS_CODE, JsonCommon.SUCCESS_MSG);
		}
       
    }
}
